#!/usr/bin/env python3
# -*- coding: utf-8-

import logging

import pygame
from pygame import display
from pygame import event
from pygame import image
from pygame import draw

class LineArt:
    def __init__(self):
        self.points = []
        
        self.current_point = None

    def add_point(self, pos):
        self.points.append(pos)

class LineDrawer:
    def __init__(self):
        self.setup()

        self.loop()

        self.quit()

    def render(self):
        if self.draw_background:
            self.screen.blit(self.unicorn_surface, [0,0])
        else:
            self.screen.fill((0,0,0))

        if len(self.lineart.points) > 1:
            draw.lines(self.screen, (0,255,0), False, self.lineart.points, 3)

    def loop(self):
        while self.event_loop():
 
            self.render()

            display.flip()

    def event_loop(self):
        for e in event.get():
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_ESCAPE:
                    logging.info('escape')
                    return False

                if e.key == pygame.K_SPACE:
                    self.draw_background = not self.draw_background
                    logging.info(self.draw_background)

                if e.key == pygame.K_p:
                    logging.info(self.lineart.points)

            if e.type == pygame.MOUSEBUTTONDOWN:
                if e.button == 1:
                    self.lineart.add_point(e.pos)

                if e.button == 3:
                    self.lineart.points.pop()

        return True

    def setup(self):
        pygame.init()                                                                           
        self.screen = display.set_mode((900, 582))

        self.unicorn_surface = image.load('unicorn.jpg')
        self.draw_background = True

        self.lineart = LineArt()

    def quit(self):
        pygame.quit()

def main():
    logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
    LineDrawer()

if __name__ == "__main__":
    main()
